using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Serialization;
using System.Drawing;
using SlimDX;
using SlimDX.Direct3D9;

using Splino.D3D;

namespace Splino
{
    /// <summary>
    /// Handles file reading and writing for the splino.xml format
    /// </summary>
    [XmlRoot("Splino_XML_0.2")]
    public class SplinoXML
    {
        /// <summary>
        /// Gets the collection of Splino Window settings
        /// </summary>
        [XmlArray("SplinoWindows")]
        [XmlArrayItem("SplinoWindow", typeof(SplinoWindowXML))]
        public SplinoWindowXML[] SplinoWindows
        {
            get { return SplinoForm.Instance.SplinoWindowsXML.ToArray(); }
            set
            {
                DeviceManager.Instance.ClearDeviceManager();
                SceneManager.Instance.ClearSceneManager();
                CommandManager.Instance.ClearCommandManager();

                List<SplinoWindow> splinoWindows = new List<SplinoWindow>();
                foreach (SplinoWindow splinoWindow in SplinoForm.Instance.SplinoWindows)
                    splinoWindows.Add(splinoWindow);
                foreach (SplinoWindow splinoWindow in splinoWindows)
                    splinoWindow.Close();
                splinoWindows.Clear();
                SplinoForm.Instance.SplinoWindows.Clear();

                foreach (SplinoWindowXML splinoWindowXML in value)
                {
                    SplinoWindow newSplinoWindow = SplinoForm.Instance.CreateSplinoWindow();
                    newSplinoWindow.Text = splinoWindowXML.Text;
                    newSplinoWindow.SplinoViewport = splinoWindowXML.SplinoViewport;
                    if (!splinoWindowXML.Docked)
                    {
                        newSplinoWindow.Undock();
                        newSplinoWindow.Location = splinoWindowXML.Location;
                        newSplinoWindow.Width = splinoWindowXML.Width;
                        newSplinoWindow.Height = splinoWindowXML.Height;
                    }
                    DeviceManager.Instance.Viewports.Add(newSplinoWindow.SplinoViewport);
                    newSplinoWindow.SplinoViewport.UpdateMatrices();
                }

                SplinoForm.Instance.LayoutSplinoWindows();

                DeviceManager deviceManager = DeviceManager.Instance;
                SceneManager sceneManager = SceneManager.Instance;
            }
        }


        /// <summary>
        /// Gets or sets a Splino Object whose children are saved to or read from a file
        /// </summary>
        public SplinoObject SceneObjects
        {
            get { return SceneManager.Instance.MasterSceneObject; }
            set 
            {
                SceneManager.Instance.MasterSceneObject = value;
                List<SplinoObject> childObjects = new List<SplinoObject>();
                foreach (SplinoObject splinoObject in value.SceneChildren)
                    childObjects.Add(splinoObject);
                foreach (SplinoObject splinoObject in childObjects)
                    splinoObject.Initialize(value);
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public SplinoXML() { }

        /// <summary>
        /// Opens a splino.xml file
        /// </summary>
        /// <param name="filename">The filename to open</param>
        public static void OpenFile(string filename)
        {
            XmlTextReader topLevelReader = null;
            topLevelReader = new XmlTextReader(filename);
            XmlSerializer splinoSerializer = new XmlSerializer(typeof(SplinoXML));
            SplinoXML splinoXML = (SplinoXML)splinoSerializer.Deserialize(topLevelReader, null);
        }

        /// <summary>
        /// Saves a splino.xml file
        /// </summary>
        /// <param name="filename">The filename to save</param>
        public static void SaveFile(String filename)
        {
            XmlTextWriter fileWriter = null;
            fileWriter = new XmlTextWriter(filename, null);

            XmlSerializer splinoSerializer = new XmlSerializer(typeof(SplinoXML));
            splinoSerializer.Serialize(fileWriter, new SplinoXML());

            fileWriter.Close();
        }
    }
}
